package com.yh.controller.admin;

import com.yh.service.ElasticsearchService;
import com.yh.utils.Constants;
import com.yh.vo.ResponseVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yuhuan
 * @date 2022/01/13
 */
@RestController
@RequestMapping("/admin/es")
public class ElasticsearchWeb {
    @DubboReference(check = false)
    private ElasticsearchService service;

    /**
     * 搜索引擎环境初始化（重置搜索引擎的数据和索引）
     * 服务地址：http://localhost:8081/admin/es/init
     */
    @RequestMapping("init")
    public ResponseVO initIndex() {
        boolean initResult = service.autoInitIndex();
        return initResult ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.RESP_SUCCESS)
                : ResponseVO.buildSuccessInstance(Constants.ResponseDataState.INDEX_INIT_FAIL);
    }

    /**
     * 删除搜索引擎的索引
     * 服务地址：http://localhost:8081/admin/es/index/delete
     */
    @RequestMapping("/index/delete")
    public ResponseVO deleteIndex() {
        boolean initResult = service.deleteIndex();
        return initResult ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.RESP_SUCCESS)
                : ResponseVO.buildSuccessInstance(Constants.ResponseDataState.INDEX_DELETE_FAIL);
    }
}
